<?php

namespace App\Http\Controllers\Api\Outward;

use Illuminate\Http\Request;

use App\Service\Rpc\Crm;

use App\Http\Controllers\Controller;

class CustomerAccountController extends Controller
{

    /**
     * 会员账户查询接口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function search( Request $request )
    {

        $card_no = $request->input('card_no'); //会员卡号
        $account_type = $request->input('account_type',1);  //账户类型,不传默认为1

        //会员卡号
        if ( empty( $card_no ) ) {
            return response()->json(['code' => 100001, 'message' => '参数card_no不能为空']);
        }

        //账户类型 1.查询积分余额 2.查询余额 3.查询积分
        if ( !in_array( $account_type,array(1,2,3) )  ) {
            return response()->json(['code' => 100002, 'message' => '参数account_type不能为空']);
        }

        //传参数组
        $args_array = [
            'card_no'=>$card_no //会员卡号
        ];

        //实例化crm
        $crm = new Crm();

        //账户信息
        $account = [];

        //查询余额
        if ( $account_type == 2 || $account_type == 1 ) {

            $balance = $crm->accountQuery( $args_array );
            if ( $balance['code'] != 200 ) {
                return response()->json(['code' => $balance['code'], 'message' => $balance['message']]);
            }
            $account['balance'] =  $balance['data']['balance']; //账户可用余额
        }

        //查询积分
        if ( $account_type == 3 || $account_type == 1 ) {

            $points = $crm->pointsQuery( $args_array );
            if ( $points['code'] != 200 ) {
                return response()->json(['code' => $points['code'], 'message' => $points['message']]);
            }
            $account['points'] = $points['data']['points']; //账户可用积分
        }


        return response()->json(['code' => 200, 'message' => 'ok', 'data' => $account]);

    }


    /**
     * 会员增加积分接口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public  function pointsAdd( Request $request )
    {

        $card_no = $request->input('card_no');  //会员名
        $points = $request->input('points');    //积分增加值
        $remark = $request->input('remark');  //备注

        //会员卡号
        if ( empty( $card_no ) ) {
            return response()->json(['code' => 100001, 'message' =>'参数card_no不能为空']);
        }

        //积分增加值
        if ( empty( $points ) ) {
            return response()->json(['code' => 100002, 'message' =>'参数points不能为空']);
        }
        if ( $points < 0 ) {
            return response()->json(['code' => 100003, 'message' =>'参数points值不能小于0']);
        }

        //传参数组
        $args_array = [
            'card_no'=>$card_no,
            'points'=>$points,
            'remark'=>$remark
        ];

        //实例化crm
        $crm = new Crm();

        //账户积分增加
        $points_data = $crm->pointsAdd( $args_array );
        if ( $points_data['code'] != 200 ) {
            return response()->json(['code' => $points_data['code'], 'message' => $points_data['message']]);
        }

        return response()->json(['code' => 200, 'message' =>'ok','data'=>$points_data['data']]);
    }


    /**
     * 会员扣减积分接口
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function pointsReduction( Request $request )
    {

        $card_no = $request->input('card_no');  //会员卡号
        $points = $request->input('points');    //积分扣减值
        $remark = $request->input('remark');  //备注

        //会员卡号
        if ( empty( $card_no ) ) {
            return response()->json(['code' => 100001, 'message' =>'参数card_no不能为空']);
        }

        //积分扣减值
        if ( empty( $points ) ) {
            return response()->json(['code' => 100002, 'message' =>'参数points不能为空']);
        }
        if ( $points < 0 ) {
            return response()->json(['code' => 100003, 'message' =>'参数points值不能小于0']);
        }

        //传参数组
        $args_array = [
            'card_no'=>$card_no,
            'points'=>$points,
            'remark'=>$remark
        ];

        //实例化crm
        $crm = new Crm();

        //账户积分扣减
        $points_data = $crm->pointsReduction( $args_array );
        if ( $points_data['code'] != 200 ) {
            return response()->json(['code' => $points_data['code'], 'message' => $points_data['message']]);
        }


        return response()->json(['code' => 200, 'message' =>'ok','data'=>$points_data['data']]);
    }


    /**
     * 会员余额充值接口
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function recharge( Request $request )
    {

        $card_no = $request->input('card_no');  //会员卡号
        $amount = $request->input('amount');    //充值金额
        $remark = $request->input('remark');  //备注

        //会员卡号
        if ( empty( $card_no ) ) {
            return response()->json(['code' => 100001, 'message' =>'参数card_no不能为空']);
        }

        //充值金额
        if ( empty( $amount ) ) {
            return response()->json(['code' => 100002, 'message' =>'参数amount不能为空']);
        }
        if ( $amount < 0 ) {
            return response()->json(['code' => 100003, 'message' =>'参数amount值不能小于0']);
        }

        //传参数组
        $args_array = [
            'card_no'=>$card_no,
            'amount'=>$amount,
            'remark'=>$remark
        ];

        //实例化crm
        $crm = new Crm();

        //账户余额充值
        $balance_data = $crm->recharge( $args_array );
        if ( $balance_data['code'] != 200 ) {
            return response()->json(['code' => $balance_data['code'], 'message' => $balance_data['message']]);
        }


        return response()->json(['code' => 200, 'message' =>'ok','data'=>$balance_data['data']]);
    }


    /**
     * 会员余额消费接口
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function consumption( Request $request )
    {

        $card_no = $request->input('card_no');  //会员卡号
        $amount = $request->input('amount');    //消费金额
        $remark = $request->input('remark');  //备注

        //会员卡号
        if ( empty( $card_no ) ) {
            return response()->json(['code' => 100001, 'message' =>'参数card_no不能为空']);
        }

        //消费金额
        if ( empty( $amount ) ) {
            return response()->json(['code' => 100002, 'message' =>'参数amount不能为空']);
        }
        if ( $amount < 0 ) {
            return response()->json(['code' => 100003, 'message' =>'参数amount值不能小于0']);
        }

        //传参数组
        $args_array = [
            'card_no'=>$card_no,
            'amount'=>$amount,
            'remark'=>$remark
        ];

        //实例化crm
        $crm = new Crm();

        //账户余额消费
        $balance_data = $crm->consumption( $args_array );
        if ( $balance_data['code'] != 200 ) {
            return response()->json(['code' => $balance_data['code'], 'message' => $balance_data['message']]);
        }


        return response()->json(['code' => 200, 'message' =>'ok','data'=>$balance_data['data']]);

    }


    /**
     * 会员账户记录接口
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function accountLogs( Request $request )
    {

        $card_no = $request->input('card_no');  //会员卡号
        $account_type = $request->input('account_type');  //账户类型 2.余额 3.积分
        $limit = $request->input('limit',10);    //每页显示条数,不传默认为10
        $page = $request->input('page',1);  //页码,不传默认为1

        //会员卡号
        if ( empty( $card_no ) ) {
            return response()->json(['code' => 100001, 'message' =>'会员卡号不能为空']);
        }

        //账户类型 2.余额 3.积分
        if ( !in_array( $account_type,array(2,3) ) ) {
            return response()->json(['code' => 100002, 'message' =>'账户类型不能为空']);
        }

        //传参数组
        $args_array = [
            'card_no'=>$card_no,
            'account_type'=>$account_type,
            'limit'=>$limit,
            'page'=>$page
        ];

        //实例化crm
        $crm = new Crm();

        //账户类型 2.余额 3.积分
        if ( $account_type == 2 ) {
            //账户余额日志查询
            $account_data = $crm->accountLogs( $args_array );

        } else {
            //账户积分日志查询
            $account_data = $crm->pointsLogs( $args_array );
        }

        if ( $account_data['code'] != 200 ) {
            return response()->json(['code' => $account_data['code'], 'message' => $account_data['message']]);
        }


        return response()->json(['code' => 200, 'message' => 'ok', 'data' => $account_data['data']]);
    }

}